home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Networking / Transition Queue Watcher / ATQWatcher.p < prev    next >
Encoding:
Text File  |  1993-01-08  |  55.9 KB  |  1,709 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    This sample program demonstrates the implementation of an
  6. #    AppleTalk Transition Queue to demonstrate the messages
  7. #    which get passed when transition events take place.
  8. #
  9. #    This sample program is based on the sample application
  10. #    TESample.p
  11. #
  12. #    MultiFinder-Aware Simple TextEdit Sample Application
  13. #
  14. #    ATQWatcher
  15. #
  16. #    ATQWatcher.p    -    Pascal Source
  17. #
  18. #    Copyright © Apple Computer, Inc. 1989-1992
  19. #    All rights reserved.
  20. #
  21. #                1.00                01/92
  22. #                1.01             01/93
  23. #
  24. #    Change History:
  25. #                1/7/93    RRK    Changed calls to LAPAddATQ and LAPRmvATQ to used
  26. #                            the fixed routines in the file ATQFix.c instead
  27. #
  28. #    Components:
  29. #                ATQWatcher.p            Feb.  1, 1992
  30. #                ATQWatcherGlue.a    Feb.  1, 1992
  31. #                ATQWatcher.r            Feb.  1, 1992
  32. #                ATQWatcher.h            Feb.  1, 1992
  33. #                ATQFix.c                    Jan.  7, 1993
  34. #                ATQWatcher.make        Feb.  1, 1992
  35. #
  36. # ATQWatcher implements a single TextEdit window for display
  37. # of messages occurring on the AppleTalkTransition Queue.  The 
  38. # following are a list of ways to generate Transition events.
  39. #
  40. # Launch ATQWatcher under multifinder and leave it in the
  41. # background.  Bring up the Chooser and shutdown and turn on
  42. # AppleTalk.  Bring ATQWatcher to the foreground and go to the
  43. # Control menu and select the "Refuse ATalk Close" item, then
  44. # try shutting down AppleTalk.  Also try using the Network cdev
  45. # to change the current AppleTalk connection.  
  46. #
  47. # With AppleTalk active, bring up the Sharing Setup cdev
  48. # and change the Macintosh name.  Bring ATQWatcher to the 
  49. # foreground and go to the Control menu and select the 
  50. # "Refuse ATalk Close" item, then try changing the
  51. # Macintosh Name.
  52. #
  53. # Establish a connection with the Remote Access program to
  54. # generate a Network Transition message.  Break the
  55. # connection.
  56. #
  57. # To generate a range change transition event, connect the system
  58. # to a network which includes a router.  Launch ATQWatcher, 
  59. # before starting up the router.
  60. #
  61. # Presently, there is no system means towards generating 
  62. # a speed transition event, however third party cdevs are
  63. # being released which toggle the 68040 cache on the Quadra
  64. # units on the fly.  If these cdev's have been written to generate
  65. # the 'sped' range change transition event, ATQWatcher will 
  66. # report the event.  To check it's operation, write a quick
  67. # program to make the
  68. # ATEvent(ATTransSpeedChange,nil);
  69. # call.
  70. #
  71. # The following are the original comments for TESample.
  72. #
  73. #    TESample is an example application that demonstrates how 
  74. #    to initialize the commonly used toolbox managers, operate 
  75. #    successfully under MultiFinder, handle desk accessories and 
  76. #    create, grow, and zoom windows. The fundamental TextEdit 
  77. #    toolbox calls and TextEdit autoscroll are demonstrated. It 
  78. #    also shows how to create and maintain scrollbar controls.
  79. #
  80. #    It does not by any means demonstrate all the techniques you 
  81. #    need for a large application. In particular, Sample does not 
  82. #    cover exception handling, multiple windows/documents, 
  83. #    sophisticated memory management, printing, or undo. All of 
  84. #    these are vital parts of a normal full-sized application.
  85. #
  86. #    This application is an example of the form of a Macintosh 
  87. #    application; it is NOT a template. It is NOT intended to be 
  88. #    used as a foundation for the next world-class, best-selling, 
  89. #    600K application. A stick figure drawing of the human body may 
  90. #    be a good example of the form for a painting, but that does not 
  91. #    mean it should be used as the basis for the next Mona Lisa.
  92. #
  93. #    We recommend that you review this program or Sample before 
  94. #    beginning a new application. Sample is a simple app. which doesn’t 
  95. #    use TextEdit or the Control Manager.
  96. #
  97. ------------------------------------------------------------------------------}
  98.  
  99.  
  100. PROGRAM ATQSample;
  101.  
  102.  
  103. {Segmentation strategy:
  104.  
  105.  This program consists of three segments. Main contains most of the code,
  106.  including the MPW libraries, and the main program. Initialize contains
  107.  code that is only used once, during startup, and can be unloaded after the
  108.  program starts. %A5Init is automatically created by the Linker to initialize
  109.  globals for the MPW libraries and is unloaded right away.}
  110.  
  111.  
  112. {SetPort strategy:
  113.  
  114.  Toolbox routines do not change the current port. In spite of this, in this
  115.  program we use a strategy of calling SetPort whenever we want to draw or
  116.  make calls which depend on the current port. This makes us less vulnerable
  117.  to bugs in other software which might alter the current port (such as the
  118.  bug (feature?) in many desk accessories which change the port on OpenDeskAcc).
  119.  Hopefully, this also makes the routines from this program more self-contained,
  120.  since they don't depend on the current port setting.}
  121.  
  122.  
  123. {Clipboard strategy:
  124.  
  125.  This program does not maintain a private scrap. Whenever a cut, copy, or paste
  126.  occurs, we import/export from the public scrap to TextEdit's scrap right away,
  127.  using the TEToScrap and TEFromScrap routines. If we did use a private scrap,
  128.  the import/export would be in the activate/deactivate event and suspend/resume
  129.  event routines.}
  130.  
  131. {$D+}
  132.  
  133. USES
  134.     MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, Traps, AppleTalk, TransQueue;
  135.  
  136.  
  137. CONST
  138.     {MPW 3.0 will include a Traps.p interface file that includes constants for trap numbers.
  139.      These constants are from that file.}
  140.     {1.02 - Uses Traps.p to obtain trap numbers.}
  141.  
  142.     {1.01 - changed constants to begin with 'k' for consistency, except for resource IDs}
  143.     {kTextMargin is the number of pixels we leave blank at the edge of the window.}
  144.     kTextMargin                = 2;
  145.  
  146.     {kMaxOpenDocuments is used to determine whether a new document can be opened
  147.      or created. We keep track of the number of open documents, and disable the
  148.      menu items that create a new document when the maximum is reached. If the
  149.      number of documents falls below the maximum, the items are enabled again.}
  150.     kMaxOpenDocuments        = 1;
  151.     
  152.     {kMaxDocWidth is an arbitrary number used to specify the width of the TERec's
  153.     destination rectangle so that word wrap and horizontal scrolling can be
  154.     demonstrated.}
  155.     kMaxDocWidth            = 576;
  156.     
  157.     {kMinDocDim is used to limit the minimum dimension of a window when GrowWindow
  158.     is called.}
  159.     kMinDocDim                = 64;
  160.     
  161.     {kControlInvisible is used to 'turn off' controls (i.e., cause the control not
  162.     to be redrawn as a result of some Control Manager call such as SetCtlValue)
  163.     by being put into the contrlVis field of the record. kControlVisible is used
  164.     the same way to 'turn on' the control.}
  165.     kControlInvisible        = 0;
  166.     kControlVisible            = $FF;
  167.     
  168.     {kScrollBarAdjust and kScrollBarWidth are used in calculating
  169.     values for control positioning and sizing.}
  170.     kScrollbarWidth            = 16;
  171.     kScrollbarAdjust        = kScrollbarWidth - 1;
  172.     
  173.     {kScrollTweek compensates for off-by-one requirements of the scrollbars
  174.      to have borders coincide with the growbox.}
  175.     kScrollTweek            = 2;
  176.     
  177.     {kCrChar is used to match with a carriage return when calculating the
  178.     number of lines in the TextEdit record. kDelChar is used to check for
  179.     delete in keyDowns.}
  180.     kCRChar                    = 13;
  181.     kDelChar                = 8;
  182.     
  183.     {kButtonScroll is how many pixels to scroll horizontally when the button part
  184.     of the horizontal scrollbar is pressed.}
  185.     kButtonScroll            = 4;
  186.     
  187.     {kMaxTELength is an arbitrary number used to limit the length of text in the TERec
  188.     so that various errors won't occur from too many characters in the text.}
  189.     kMaxTELength            = 32000;
  190.  
  191.     {kSysEnvironsVersion is passed to SysEnvirons to tell it which version of the
  192.      SysEnvRec we understand.}
  193.     kSysEnvironsVersion        = 1;
  194.  
  195.     {kOSEvent is the event number of the suspend/resume and mouse-moved events sent
  196.      by MultiFinder. Once we determine that an event is an osEvent, we look at the
  197.      high byte of the message sent to determine which kind it is. To differentiate
  198.      suspend and resume events we check the resumeMask bit.}
  199.     kOSEvent                            = app4Evt;    {event used by MultiFinder}
  200.     kSuspendResumeMessage    = 1;        {high byte of suspend/resume event message}
  201.     kResumeMask                    = 1;        {bit of message field for resume vs. suspend}
  202.     kMouseMovedMessage        = $FA;    {high byte of mouse-moved event message}
  203.     kNoEvents                            = 0;        {no events mask}
  204.     
  205.     kReturnChar                        = 13;    {char code for the return character}
  206.  
  207.     {1.01 - kMinHeap - This is the minimum result from the following
  208.      equation:
  209.             
  210.             ORD(GetApplLimit) - ORD(ApplicZone)
  211.             
  212.      for the application to run. It will insure that enough memory will
  213.      be around for reasonable-sized scraps, FKEYs, etc. to exist with the
  214.      application, and still give the application some 'breathing room'.
  215.      To derive this number, we ran under a MultiFinder partition that was
  216.      our requested minimum size, as given in the 'SIZE' resource.}
  217.      
  218.     kMinHeap    = 29 * 1024;
  219.     
  220.     {1.01 - kMinSpace - This is the minimum result from PurgeSpace, when called
  221.      at initialization time, for the application to run. This number acts
  222.      as a double-check to insure that there really is enough memory for the
  223.      application to run, including what has been taken up already by
  224.      pre-loaded resources, the scrap, code, and other sundry memory blocks.}
  225.      
  226.     kMinSpace    = 20 * 1024;
  227.     
  228.     {kExtremeNeg and kExtremePos are used to set up wide open rectangles and regions.}
  229.     kExtremeNeg        = -32768;
  230.     kExtremePos        = 32767 - 1;    {required for old region bug}
  231.     
  232.     {kTESlop provides some extra security when pre-flighting edit commands.}
  233.     kTESlop            = 1024;
  234.  
  235.     {kErrStrings is the resource ID for the error strings STR# resource.}
  236.     kErrStrings        = 128;
  237.  
  238.     {The following are indicies into STR# resources.}
  239.     eWrongMachine    = 1;
  240.     eSmallSize        = 2;
  241.     eNoMemory        = 3;
  242.     eNoSpaceCut        = 4;
  243.     eNoCut                = 5;
  244.     eNoCopy            = 6;
  245.     eExceedPaste    = 7;
  246.     eNoSpacePaste    = 8;
  247.     eNoWindow        = 9;
  248.     eExceedChar        = 10;
  249.     eNoPaste            = 11;
  250.     eNoLAPMgr        = 12;
  251.     
  252.     {The following constants are all resource IDs, corresponding to resources in Sample.r.}
  253.     rMenuBar        = 128;                    {application's menu bar}
  254.     rAboutAlert    = 128;                    {about alert}
  255.     rUserAlert    = 129;                    {user error alert}
  256.     rDocWindow    = 128;                    {application's window}
  257.     rVScroll        = 128;                    {vertical scrollbar control}
  258.     rHScroll        = 129;                    {horizontal scrollbar control}
  259.  
  260.     {The following constants are used to identify menus and their items. The menu IDs
  261.      have an "m" prefix and the item numbers within each menu have an "i" prefix.}
  262.     mApple        = 128;                    {Apple menu}
  263.     iAbout        = 1;
  264.  
  265.     mFile        = 129;                    {File menu}
  266.     iNew            = 1;
  267.     iClose        = 4;
  268.     iQuit            = 12;
  269.  
  270.     mEdit        = 130;                    {Edit menu}
  271.     iUndo        = 1;
  272.     iCut            = 3;
  273.     iCopy        = 4;
  274.     iPaste        = 5;
  275.     iClear        = 6;
  276.     
  277.     mControl            = 131;                    {Control menu}
  278.     iNoMPPClose        = 1;
  279.     iNoNameChange = 2;
  280.     
  281.  
  282.     {1.01 - kDITop and kDILeft are used to locate the Disk Initialization dialogs.}
  283.     kDITop        = $0050;
  284.     kDILeft        = $0070;
  285.  
  286.  
  287. TYPE
  288.     {A DocumentRecord contains the WindowRecord for one of our document windows,
  289.      as well as the TEHandle for the text we are editing. We have added fields to
  290.      hold the ControlHandles to the vertical and horizontal scrollbars and to hold
  291.      the address of the default clikLoop that gets attached to a TERec when you call
  292.      TEAutoView. Other document fields can be added to this record as needed. For
  293.      a similar example, see how the Window Manager and Dialog Manager add fields
  294.      after the GrafPort.}
  295.     DocumentRecord    = RECORD
  296.         docWindow    : WindowRecord;
  297.         docTE            : TEHandle;
  298.         docVScroll    : ControlHandle;
  299.         docHScroll    : ControlHandle;
  300.         docClik            : ProcPtr;
  301.     END;
  302.     DocumentPeek    = ^DocumentRecord;
  303.  
  304.  
  305. VAR
  306.     {The "g" prefix is used to emphasize that a variable is global.}
  307.  
  308.     {GMac is used to hold the result of a SysEnvirons call. This makes
  309.      it convenient for any routine to check the environment. It is
  310.      global information, anyway.}
  311.     gMac                : SysEnvRec;    {set up by Initialize}
  312.  
  313.     {GHasWaitNextEvent is set at startup, and tells whether the WaitNextEvent
  314.      trap is available. If it is false, we know that we must call GetNextEvent.}
  315.     gHasWaitNextEvent    : BOOLEAN;        {set up by Initialize}
  316.  
  317.     {GInBackground is maintained by our OSEvent handling routines. Any part of
  318.      the program can check it to find out if it is currently in the background.}
  319.     gInBackground        : BOOLEAN;        {maintained by Initialize and DoEvent}
  320.  
  321.     {GNumDocuments is used to keep track of how many open documents there are
  322.      at any time. It is maintained by the routines that open and close documents.}
  323.     gNumDocuments        : INTEGER;        {maintained by Initialize, DoNew, and DoCloseWindow}
  324.     
  325.     {GATQEntry is used to set the pass the sample Transition Queue Handler to 
  326.      AppleTalk.}
  327.     gATQEntry            : myATQentry;
  328.     
  329. {$S Initialize}
  330. FUNCTION TrapAvailable(tNumber: INTEGER; tType: TrapType): BOOLEAN;
  331.  
  332. {Check to see if a given trap is implemented. This is only used by the
  333.  Initialize routine in this program, so we put it in the Initialize segment.
  334.  The recommended approach to see if a trap is implemented is to see if
  335.  the address of the trap routine is the same as the address of the
  336.  Unimplemented trap.}
  337. {1.02 - Needs to be called after call to SysEnvirons so that it can check
  338.  if a ToolTrap is out of range of a pre-MacII ROM.}
  339.  
  340. BEGIN
  341.     IF (tType = ToolTrap) &
  342.         (gMac.machineType > envMachUnknown) &
  343.         (gMac.machineType < envMacII) THEN BEGIN        {it's a 512KE, Plus, or SE}
  344.         tNumber := BAND(tNumber, $03FF);
  345.         IF tNumber > $01FF THEN                            {which means the tool traps}
  346.             tNumber := _Unimplemented;                    {only go to $01FF}
  347.     END;
  348.     TrapAvailable := NGetTrapAddress(tNumber, tType) <>
  349.                         GetTrapAddress(_Unimplemented);
  350. END; {TrapAvailable}
  351.  
  352.  
  353. {$S Main}
  354. FUNCTION IsDAWindow(window: WindowPtr): BOOLEAN;
  355.  
  356. {Check if a window belongs to a desk accessory.}
  357.  
  358. BEGIN
  359.     IF window = NIL THEN
  360.         IsDAWindow := FALSE
  361.     ELSE    {DA windows have negative windowKinds}
  362.         IsDAWindow := WindowPeek(window)^.windowKind < 0;
  363. END; {IsDAWindow}
  364.  
  365.  
  366. {$S Main}
  367. FUNCTION IsAppWindow(window: WindowPtr): BOOLEAN;
  368.  
  369. {Check to see if a window belongs to the application. If the window pointer
  370.  passed was NIL, then it could not be an application window. WindowKinds
  371.  that are negative belong to the system and windowKinds less than userKind
  372.  are reserved by Apple except for windowKinds equal to dialogKind, which
  373.  mean it is a dialog.
  374.  1.02 - In order to reduce the chance of accidentally treating some window
  375.  as an AppWindow that shouldn't be, we'll only return true if the windowkind
  376.  is userKind. If you add different kinds of windows to Sample you'll need
  377.  to change how this all works.}
  378.  
  379. BEGIN
  380.     IF window = NIL THEN
  381.         IsAppWindow := FALSE
  382.     ELSE    {application windows have windowKinds = userKind (8)}
  383.         WITH WindowPeek(window)^ DO
  384.             IsAppWindow := (windowKind = userKind);
  385. END; {IsAppWindow}
  386.  
  387.  
  388. {$S Main}
  389. PROCEDURE AlertUser(error: INTEGER);
  390.  
  391. {Display an alert that tells the user an error occurred, then exit the program.
  392.  This routine is used as an ultimate bail-out for serious errors that prohibit
  393.  the continuation of the application. Errors that do not require the termination
  394.  of the application should be handled in a different manner. Error checking and
  395.  reporting has a place even in the simplest application. The error number is used
  396.  to index an 'STR#' resource so that a relevant message can be displayed.}
  397.  
  398. VAR
  399.     itemHit    : INTEGER;
  400.     message    : Str255;
  401. BEGIN
  402.     SetCursor(arrow);
  403.     GetIndString(message, kErrStrings, error);
  404.     ParamText(message, '', '', '');
  405.     itemHit := Alert(rUserAlert, NIL);
  406. END; {AlertUser}
  407.  
  408.  
  409. {$S Main}
  410. PROCEDURE GetTERect(window: WindowPtr; VAR teRect: Rect);
  411.  
  412. {return a rectangle that is inset from the portRect by the size of
  413. the scrollbars and a little extra margin.}
  414.  
  415. BEGIN
  416.     teRect := window^.portRect;
  417.     InsetRect(teRect, kTextMargin, kTextMargin);            {adjust for margin}
  418.     teRect.bottom := teRect.bottom - kScrollbarAdjust;    {and for the scrollbars}
  419.     teRect.right := teRect.right - kScrollbarAdjust;
  420. END; {GetTERect}
  421.  
  422.  
  423. {$S Main}
  424. FUNCTION DoCloseWindow(window: WindowPtr) : BOOLEAN;
  425.  
  426. {Close a window. This handles desk accessory and application windows.}
  427.  
  428. {1.01 - At this point, if there was a document associated with a
  429.  window, you could do any document saving processing if it is 'dirty'.
  430.  DoCloseWindow would return TRUE if the window actually closed, i.e.,
  431.  the user didn’t cancel from a save dialog. This result is handy when
  432.  the user quits an application, but then cancels the save of a document
  433.  associated with a window.}
  434.  
  435. BEGIN
  436.     DoCloseWindow := TRUE;
  437.     IF IsDAWindow(window) THEN
  438.         CloseDeskAcc(WindowPeek(window)^.windowKind)
  439.     ELSE IF IsAppWindow(window) THEN BEGIN
  440.         WITH DocumentPeek(window)^ DO
  441.             IF docTE <> NIL THEN BEGIN
  442.                 TEDispose(docTE);        {dispose the TEHandle}
  443.                 if  (gATQEntry.globs^.docTE <> nil) THEN
  444.                     gATQEntry.globs^.docTE := NIL;
  445.             END;
  446.         {1.01 - We used to call DisposeWindow, but that was technically
  447.          incorrect, even though we allocated storage for the window on
  448.          the heap. We should instead call CloseWindow to have the structures
  449.          taken care of and then dispose of the storage ourselves.}
  450.         CloseWindow(window);
  451.         DisposPtr(Ptr(window));
  452.         gNumDocuments := gNumDocuments - 1;
  453.     END;
  454. END; {DoCloseWindow}
  455.  
  456.  
  457. {$S Main}
  458. PROCEDURE AdjustViewRect(docTE: TEHandle);
  459.  
  460. {Update the TERec's view rect so that it is the greatest multiple of
  461. the lineHeight and still fits in the old viewRect.}
  462.  
  463. BEGIN
  464.     WITH docTE^^ DO BEGIN
  465.         viewRect.bottom := (((viewRect.bottom - viewRect.top) DIV lineHeight) *
  466.                             lineHeight) + viewRect.top;
  467.     END;
  468. END; {AdjustViewRect}
  469.  
  470.  
  471. {$S Main}
  472. PROCEDURE AdjustTE(window: WindowPtr);
  473.  
  474. {Scroll the TERec around to match up to the potentially updated scrollbar
  475. values. This is really useful when the window resizes such that the
  476. scrollbars become inactive and the TERec had been previously scrolled.}
  477.  
  478. VAR
  479.     value    : INTEGER;
  480. BEGIN
  481.     WITH DocumentPeek(window)^ DO BEGIN
  482.         TEScroll((docTE^^.viewRect.left - docTE^^.destRect.left) - GetCtlValue(docHScroll),
  483.                 (docTE^^.viewRect.top - docTE^^.destRect.top) -
  484.                     (GetCtlValue(docVScroll) * docTE^^.lineHeight),
  485.                 docTE);
  486.     END;
  487. END; {AdjustTE}
  488.  
  489.  
  490. {$S Main}
  491. PROCEDURE AdjustHV(isVert: BOOLEAN; control: ControlHandle; docTE: TEHandle; canRedraw: BOOLEAN);
  492.  
  493. {Calculate the new control maximum value and current value, whether it is the horizontal or
  494. vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  495. vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  496. width to the width of the viewRect. The current values are set by comparing the offset between
  497. the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  498. calling ShowControl.}
  499.  
  500. VAR
  501.     value, lines, max    : INTEGER;
  502.     oldValue, oldMax    : INTEGER;
  503. BEGIN
  504.     oldValue := GetCtlValue(control);
  505.     oldMax := GetCtlMax(control);
  506.     IF isVert THEN BEGIN
  507.         lines := docTE^^.nLines;
  508.         {since nLines isn’t right if the last character is a return, check for that case}
  509.         IF Ptr(ORD(docTE^^.hText^) + docTE^^.teLength - 1)^ = kCRChar THEN
  510.             lines := lines + 1;
  511.         max := lines - ((docTE^^.viewRect.bottom - docTE^^.viewRect.top) DIV docTE^^.lineHeight);
  512.     END ELSE
  513.         max := kMaxDocWidth - (docTE^^.viewRect.right - docTE^^.viewRect.left);
  514.     IF max < 0 THEN max := 0;            {check for negative values}
  515.     SetCtlMax(control, max);
  516.     IF isVert THEN
  517.         value := (docTE^^.viewRect.top - docTE^^.destRect.top) DIV docTE^^.lineHeight
  518.     ELSE
  519.         value := docTE^^.viewRect.left - docTE^^.destRect.left;
  520.     IF value < 0 THEN
  521.         value := 0
  522.     ELSE IF value > max THEN
  523.         value := max;                    {pin the value to within range}
  524.     SetCtlValue(control, value);
  525.     IF canRedraw & ( (max <> oldMax) | (value <> oldValue) ) THEN
  526.         ShowControl(control);            {check to see if the control can be re-drawn}
  527. END; {AdjustHV}
  528.  
  529.  
  530. {$S Main}
  531. PROCEDURE AdjustScrollValues(window: WindowPtr; canRedraw: BOOLEAN);
  532.  
  533. {Simply call the common adjust routine for the vertical and horizontal scrollbars.}
  534.  
  535. BEGIN
  536.     WITH DocumentPeek(window)^ DO BEGIN
  537.         AdjustHV(TRUE, docVScroll, docTE, canRedraw);
  538.         AdjustHV(FALSE, docHScroll, docTE, canRedraw);
  539.     END;
  540. END; {AdjustScrollValues}
  541.  
  542.  
  543. {$S Main}
  544. PROCEDURE AdjustScrollSizes(window: WindowPtr);
  545.  
  546. {Re-calculate the position and size of the viewRect and the scrollbars.
  547.  kScrollTweek compensates for off-by-one requirements of the scrollbars
  548.  to have borders coincide with the growbox.}
  549.  
  550. VAR
  551.     teRect    : Rect;
  552. BEGIN
  553.     GetTERect(window, teRect);                                        {start with teRect}
  554.     WITH DocumentPeek(window)^, window^.portRect DO BEGIN
  555.         docTE^^.viewRect := teRect;
  556.         AdjustViewRect(docTE);                                        {snap to nearest line}
  557.         MoveControl(docVScroll, right - kScrollbarAdjust, -1);
  558.         SizeControl(docVScroll, kScrollbarWidth, (bottom - top) -
  559.                         (kScrollbarAdjust - kScrollTweek));
  560.         MoveControl(docHScroll, -1, bottom - kScrollbarAdjust);
  561.         SizeControl(docHScroll, (right - left) - (kScrollbarAdjust -
  562.                         kScrollTweek), kScrollbarWidth);
  563.     END;
  564. END; {AdjustScrollSizes}
  565.  
  566.  
  567. {$S Main}
  568. PROCEDURE AdjustScrollbars(window: WindowPtr; needsResize: BOOLEAN);
  569.  
  570. {Turn off the controls by jamming a zero into their contrlVis fields (HideControl erases them
  571. and we don't want that). If the controls are to be resized as well, call the procedure to do that,
  572. then call the procedure to adjust the maximum and current values. Finally re-enable the controls
  573. by jamming a $FF in their contrlVis fields.}
  574.  
  575. VAR
  576.     oldMax, oldVal    : INTEGER;
  577. BEGIN
  578.     WITH DocumentPeek(window)^ DO BEGIN
  579.         {First, turn visibility of scrollbars off so we won’t get unwanted redrawing}
  580.         docVScroll^^.contrlVis := kControlInvisible;    {turn them off}
  581.         docHScroll^^.contrlVis := kControlInvisible;
  582.         IF needsResize THEN                                {move and size if needed}
  583.             AdjustScrollSizes(window);
  584.         AdjustScrollValues(window, NOT needsResize);    {fool with max and current value}
  585.         {Now, restore visibility in case we never had to ShowControl during adjustment}
  586.         docVScroll^^.contrlVis := kControlVisible;        {turn them on}
  587.         docHScroll^^.contrlVis := kControlVisible;
  588.     END;
  589. END; {AdjustScrollbars}
  590.  
  591.  
  592. {$S Main}
  593. {$PUSH} {$Z+}
  594. PROCEDURE PascalClikLoop;
  595.  
  596. {Gets called from our assembly language routine, AsmClikLoop, which is in
  597.  turn called by the TEClick toolbox routine. Saves the windows clip region,
  598.  sets it to the portRect, adjusts the scrollbar values to match the TE scroll
  599.  amount, then restores the clip region.}
  600.  
  601. VAR
  602.     window    : WindowPtr;
  603.     region    : RgnHandle;
  604. BEGIN
  605.     window := FrontWindow;
  606.     region := NewRgn;
  607.     GetClip(region);                    {save the old clip}
  608.     ClipRect(window^.portRect);            {set the new clip}
  609.     AdjustScrollValues(window, TRUE);    {pass TRUE for canRedraw}
  610.     SetClip(region);                    {restore the old clip}
  611.     DisposeRgn(region);
  612. END; {PascalClikLoop}
  613. {$POP}
  614.  
  615.  
  616. {$S Main}
  617. {$PUSH} {$Z+}
  618. FUNCTION GetOldClikLoop : ProcPtr;
  619.  
  620. {Gets called from our assembly language routine, AsmClikLoop, which is in
  621. turn called by the TEClick toolbox routine. It returns the address of the
  622. default clikLoop routine that was put into the TERec by TEAutoView to
  623. AsmClikLoop so that it can call it.}
  624.  
  625. BEGIN
  626.     GetOldClikLoop := DocumentPeek(FrontWindow)^.docClik;
  627. END; {GetOldClikLoop}
  628. {$POP}
  629.  
  630.  
  631. PROCEDURE AsmClikLoop; EXTERNAL;
  632.  
  633. {A reference to our assembly language routine that gets attached to the clikLoop
  634. field of our TE record.}
  635.  
  636.  
  637. {$S Main}
  638. PROCEDURE DoNew;
  639.  
  640. {Create a new document and window.}
  641.  
  642. VAR
  643.     good, ignore        : BOOLEAN;
  644.     storage                : Ptr;
  645.     window                : WindowPtr;
  646.     destRect, viewRect    : Rect;
  647.  
  648. BEGIN
  649.     storage := NewPtr(SIZEOF(DocumentRecord));
  650.     IF storage <> NIL THEN BEGIN
  651.         window := GetNewWindow(rDocWindow, storage, WindowPtr(-1));
  652.         IF window <> NIL THEN BEGIN
  653.             gNumDocuments := gNumDocuments + 1;    {this will be decremented when we call DoCloseWindow}
  654.             good := FALSE;
  655.             SetPort(window);
  656.             WITH window^, DocumentPeek(window)^ DO BEGIN
  657.                 GetTERect(window, viewRect);
  658.                 destRect := viewRect;
  659.                 destRect.right := destRect.left + kMaxDocWidth;
  660.                 docTE := TENew(destRect, viewRect);
  661.                 IF docTE <> NIL THEN BEGIN        {1.02 - moved}
  662.                     good := TRUE;                {if TENew succeeded, we have a good document}
  663.                     AdjustViewRect(docTE);
  664.                     TEAutoView(TRUE, docTE);
  665.                     docClik := docTE^^.clikLoop;
  666.                     docTE^^.clikLoop := @AsmClikLoop;
  667.                     if (gATQEntry.globs^.docTE = nil ) THEN
  668.                         gATQEntry.globs^.docTE := docTE;    {store TEHandle in the ATQ Globals record }
  669.                 END;
  670.                 IF good THEN BEGIN
  671.                     docVScroll := GetNewControl(rVScroll, window);
  672.                     good := (docVScroll <> NIL);
  673.                 END;
  674.                 IF good THEN BEGIN
  675.                     docHScroll := GetNewControl(rHScroll, window);
  676.                     good := (docHScroll <> NIL);
  677.                 END;
  678.                 IF good THEN BEGIN
  679.                     {FALSE to AdjustScrollValues means musn’t redraw; technically, of course,
  680.                     the window is hidden so it wouldn’t matter whether we called ShowControl or not.}
  681.                     AdjustScrollValues(window, FALSE);
  682.                     ShowWindow(window);            {if the document is good, make the window visible}
  683.                 END ELSE BEGIN
  684.                     ignore := DoCloseWindow(window);    {otherwise regret we ever created it...}
  685.                     AlertUser(eNoWindow);                {and tell user}
  686.                 END
  687.             END;
  688.         END ELSE
  689.             DisposPtr(storage);                    {get rid of the storage if it is never used}
  690.     END;
  691. END; {DoNew}
  692.  
  693.  
  694. {$S Main}
  695. PROCEDURE BigBadError(error: INTEGER);
  696. BEGIN
  697.     AlertUser(error);
  698.     ExitToShell;
  699. END;
  700.  
  701. PROCEDURE CallTransQueue; EXTERNAL;
  702. FUNCTION LAPAddATQFix(theATQEntry: ATQEntryPtr) : OSErr; EXTERNAL;
  703. FUNCTION LAPRmvATQFix(theATQEntry: ATQEntryPtr): OSErr; EXTERNAL;
  704.  
  705. {$S Initialize}
  706. PROCEDURE Initialize;
  707.  
  708. {Set up the whole world, including global variables, Toolbox managers,
  709.  menus, and a single blank document.}
  710.  
  711. {1.01 - The code that used to be part of ForceEnvirons has been moved into
  712.  this module. If an error is detected, instead of merely doing an ExitToShell,
  713.  which leaves the user without much to go on, we call AlertUser, which puts
  714.  up a simple alert that just says an error occurred and then calls ExitToShell.
  715.  Since there is no other cleanup needed at this point if an error is detected,
  716.  this form of error- handling is acceptable. If more sophisticated error recovery
  717.  is needed, an exception mechanism, such as is provided by Signals, can be used.}
  718.  
  719. VAR
  720.     menuBar                : Handle;
  721.     total, contig        : LongInt;
  722.     ignoreResult        : BOOLEAN;
  723.     event                : EventRecord;
  724.     count, ignoreError    : INTEGER;
  725.     err                    : OSErr;
  726.     
  727.     PROCEDURE BigBadError(error: INTEGER);
  728.     BEGIN
  729.         AlertUser(error);
  730.         ExitToShell;
  731.     END;
  732.  
  733. BEGIN
  734.     gInBackground := FALSE;
  735.  
  736.     InitGraf(@thePort);
  737.     InitFonts;
  738.     InitWindows;
  739.     InitMenus;
  740.     TEInit;
  741.     InitDialogs(NIL);
  742.     InitCursor;
  743.     
  744.     {This next bit of code is necessary to allow the default button of our
  745.      alert be outlined.
  746.      1.02 - Changed to call EventAvail so that we don't lose some important
  747.      events.}
  748.      
  749.     FOR count := 1 TO 3 DO
  750.         ignoreResult := EventAvail(everyEvent, event);
  751.     
  752.     {Ignore the error returned from SysEnvirons; even if an error occurred,
  753.      the SysEnvirons glue will fill in the SysEnvRec. You can save a redundant
  754.      call to SysEnvirons by calling it after initializing AppleTalk.}
  755.      
  756.     ignoreError := SysEnvirons(kSysEnvironsVersion, gMac);
  757.     
  758.     {Make sure that the machine has at least 128K ROMs. If it doesn't, exit.}
  759.     
  760.     IF gMac.machineType < 0 THEN BigBadError(eWrongMachine);
  761.     
  762.     {1.02 - Move TrapAvailable call to after SysEnvirons so that we can tell
  763.      in TrapAvailable if a tool trap value is out of range.}
  764.      
  765.     gHasWaitNextEvent := TrapAvailable(_WaitNextEvent, ToolTrap);
  766.  
  767.     {1.01 - We used to make a check for memory at this point by examining ApplLimit,
  768.      ApplicZone, and StackSpace and comparing that to the minimum size we told
  769.      MultiFinder we needed. This did not work well because it assumed too much about
  770.      the relationship between what we asked MultiFinder for and what we would actually
  771.      get back, as well as how to measure it. Instead, we will use an alternate
  772.      method comprised of two steps.}
  773.      
  774.     {It is better to first check the size of the application heap against a value
  775.      that you have determined is the smallest heap the application can reasonably
  776.      work in. This number should be derived by examining the size of the heap that
  777.      is actually provided by MultiFinder when the minimum size requested is used.
  778.      The derivation of the minimum size requested from MultiFinder is described
  779.      in Sample.h. The check should be made because the preferred size can end up
  780.      being set smaller than the minimum size by the user. This extra check acts to
  781.      insure that your application is starting from a solid memory foundation.}
  782.      
  783.     IF ORD(GetApplLimit) - ORD(ApplicZone) < kMinHeap THEN BigBadError(eSmallSize);
  784.     
  785.     {Next, make sure that enough memory is free for your application to run. It
  786.      is possible for a situation to arise where the heap may have been of required
  787.      size, but a large scrap was loaded which left too little memory. To check for
  788.      this, call PurgeSpace and compare the result with a value that you have determined
  789.      is the minimum amount of free memory your application needs at initialization.
  790.      This number can be derived several different ways. One way that is fairly
  791.      straightforward is to run the application in the minimum size configuration
  792.      as described previously. Call PurgeSpace at initialization and examine the value
  793.      returned. However, you should make sure that this result is not being modified
  794.      by the scrap's presence. You can do that by calling ZeroScrap before calling
  795.      PurgeSpace. Make sure to remove that call before shipping, though.}
  796.      
  797.     {* ZeroScrap; *}
  798.     PurgeSpace(total, contig);
  799.     IF total < kMinSpace THEN
  800.         IF UnloadScrap <> noErr THEN
  801.             BigBadError(eNoMemory)
  802.         ELSE BEGIN
  803.             PurgeSpace(total, contig);
  804.             IF total < kMinSpace THEN
  805.                 BigBadError(eNoMemory);
  806.         END;
  807.  
  808.     {The extra benefit to waiting until after the Toolbox Managers have been initialized
  809.      to check memory is that we can now give the user an alert to tell her/him what
  810.      happened. Although it is possible that the memory situation could be worsened by
  811.      displaying an alert, MultiFinder would gracefully exit the application with
  812.      an informative alert if memory became critical. Here we are acting more
  813.      in a preventative manner to avoid future disaster from low-memory problems.}
  814.  
  815.     menuBar := GetNewMBar(rMenuBar);        {read menus into menu bar}
  816.     IF menuBar = NIL THEN
  817.         BigBadError(eNoMemory);
  818.     SetMenuBar(menuBar);                    {install menus}
  819.     DisposHandle(menuBar);
  820.     AddResMenu(GetMHandle(mApple), 'DRVR');    {add DA names to Apple menu}
  821.     DrawMenuBar;
  822.  
  823.     gNumDocuments := 0;
  824.  
  825.     err := InitTransEventRec(gATQEntry.globs); { initialize the TransEventRec }
  826.     if (err <> noErr) THEN
  827.         BigBadError(eNoMemory);
  828.     gATQEntry.CallAddr := ProcPtr(@CallTransQueue);
  829.     Debugger;
  830.     err := LAPAddATQFix(ATQEntryPtr(@gATQEntry));  { add our handler to the Trans Queue }
  831.     if (err <> noErr) THEN
  832.         BigBadError(eNoLAPMgr);        { Cannot install ATQ handler }
  833.  
  834.     DoNew;                                    {create a single empty document}
  835. END; {Initialize}
  836.  
  837.  
  838. (**************************************************************************************
  839. 1.01 - PROCEDURE DoCloseBehind(window: WindowPtr); was removed.
  840.  
  841. {1.01 - DoCloseBehind was a good idea for closing windows when quitting
  842.  and not having to worry about updating the windows, but it suffered
  843.  from a fatal flaw. If a desk accessory owned two windows, it would
  844.  close both those windows when CloseDeskAcc was called. When DoCloseBehind
  845.  got around to calling DoCloseWindow for that other window that was already
  846.  closed, things would go very poorly. Another option would be to have a
  847.  procedure, GetRearWindow, that would go through the window list and return
  848.  the last window. Instead, we decided to present the standard approach
  849.  of getting and closing FrontWindow until FrontWindow returns NIL. This
  850.  has a potential benefit in that the window whose document needs to be saved
  851.  may be visible since it is the front window, therefore decreasing the
  852.  chance of user confusion. For aesthetic reasons, the windows in the
  853.  application should be checked for updates periodically and have the
  854.  updates serviced.}
  855. **************************************************************************************)
  856.  
  857.  
  858. {$S Main}
  859. PROCEDURE Terminate;
  860.  
  861. {Clean up the application and exit. We close all of the windows so that
  862.  they can update their documents, if any.}
  863.  
  864. {1.01 - If we find out that a cancel has occurred, we won't exit to the
  865.  shell, but will return instead.}
  866.  
  867. VAR
  868.     aWindow    : WindowPtr;
  869.     closed    : BOOLEAN;
  870.     err        : OSErr;
  871.  
  872. BEGIN
  873.     closed := TRUE;
  874.     REPEAT
  875.         aWindow := FrontWindow;                    {get the current front window}
  876.         IF aWindow <> NIL THEN
  877.             closed := DoCloseWindow(aWindow);    {close this window}
  878.     UNTIL (NOT closed) | (aWindow = NIL);        {do all windows}
  879.     Debugger;
  880.     err := LAPRmvATQFix(ATQEntryPtr(@gATQEntry));
  881.     IF closed THEN
  882.         ExitToShell;                            {exit if no cancellation}
  883. END; {Terminate}
  884.  
  885.  
  886. {$S Main}
  887. PROCEDURE AdjustMenus;
  888.  
  889. {Enable and disable menus based on the current state.
  890.  The user can only select enabled menu items. We set up all the menu items
  891.  before calling MenuSelect or MenuKey, since these are the only times that
  892.  a menu item can be selected. Note that MenuSelect is also the only time
  893.  the user will see menu items. This approach to deciding what enable/
  894.  disable state a menu item has the advantage of concentrating all the decision-
  895.  making in one routine, as opposed to being spread throughout the application.
  896.  Other application designs may take a different approach that may or may not be
  897.  as valid.}
  898.  
  899. VAR
  900.     window            : WindowPtr;
  901.     menu            : MenuHandle;
  902.     offset            : LONGINT;
  903.     undo            : BOOLEAN;
  904.     cutCopyClear    : BOOLEAN;
  905.     paste            : BOOLEAN;
  906.  
  907. BEGIN
  908.     window := FrontWindow;
  909.  
  910.     menu := GetMHandle(mFile);
  911.     IF gNumDocuments < kMaxOpenDocuments THEN
  912.         EnableItem(menu, iNew)        {New is enabled when we can open more documents}
  913.     ELSE
  914.         DisableItem(menu, iNew);
  915.     IF window <> NIL THEN            {Close is enabled when there is a window to close}
  916.         EnableItem(menu, iClose)
  917.     ELSE
  918.         DisableItem(menu, iClose);
  919.  
  920.     menu := GetMHandle(mEdit);
  921.     undo := FALSE;
  922.     cutCopyClear := FALSE;
  923.     paste := FALSE;
  924.     IF IsDAWindow(window) THEN BEGIN
  925.         undo := TRUE;                {all editing is enabled for DA windows}
  926.         cutCopyClear := TRUE;
  927.         paste := TRUE;
  928.     END ELSE IF IsAppWindow(window) THEN BEGIN
  929.         WITH DocumentPeek(window)^.docTE^^ DO
  930.             IF selStart < selEnd THEN
  931.                 cutCopyClear := TRUE;
  932.                 {Cut, Copy, and Clear is enabled for app. windows with selections}
  933.         IF GetScrap(NIL, 'TEXT', offset) > 0 THEN
  934.             paste := TRUE;            {Paste is enabled for app. windows}
  935.     END;
  936.     IF undo THEN
  937.         EnableItem(menu, iUndo)
  938.     ELSE
  939.         DisableItem(menu, iUndo);
  940.     IF cutCopyClear THEN BEGIN
  941.         EnableItem(menu, iCut);
  942.         EnableItem(menu, iCopy);
  943.         EnableItem(menu, iClear);
  944.     END ELSE BEGIN
  945.         DisableItem(menu, iCut);
  946.         DisableItem(menu, iCopy);
  947.         DisableItem(menu, iClear);
  948.     END;
  949.     IF paste THEN
  950.         EnableItem(menu, iPaste)
  951.     ELSE
  952.         DisableItem(menu, iPaste);
  953. END; {AdjustMenus}
  954.  
  955.  
  956. {$S Main}
  957. PROCEDURE DoMenuCommand(menuResult: LONGINT);
  958.  
  959. {This is called when an item is chosen from the menu bar (after calling
  960.  MenuSelect or MenuKey). It does the right thing for each command.}
  961.  
  962. VAR
  963.     menuID, menuItem        : INTEGER;
  964.     itemHit, daRefNum        : INTEGER;
  965.     daName                    : Str255;
  966.     ignoreResult, saveErr    : OSErr;
  967.     handledByDA                : BOOLEAN;
  968.     te                        : TEHandle;
  969.     window                    : WindowPtr;
  970.     ignore                    : BOOLEAN;
  971.     aHandle                    : Handle;
  972.     oldSize, newSize        : LongInt;
  973.     total, contig            : LongInt;
  974.  
  975. BEGIN
  976.     window := FrontWindow;
  977.     menuID := HiWrd(menuResult);    {use built-ins (for efficiency)...}
  978.     menuItem := LoWrd(menuResult);    {to get menu item number and menu number}
  979.     CASE menuID OF
  980.     
  981.         mApple:
  982.             CASE menuItem OF
  983.                 iAbout:                {bring up alert for About}
  984.                     itemHit := Alert(rAboutAlert, NIL);
  985.                 OTHERWISE BEGIN        {all non-About items in this menu are DAs}
  986.                     GetItem(GetMHandle(mApple), menuItem, daName);
  987.                     daRefNum := OpenDeskAcc(daName);
  988.                 END;
  989.             END;
  990.             
  991.         mFile:
  992.             CASE menuItem OF
  993.                 iNew:
  994.                     DoNew;
  995.                 iClose:
  996.                     ignore := DoCloseWindow(window); {we don't care if cancelled}
  997.                 iQuit:
  998.                     Terminate;
  999.             END;
  1000.             
  1001.         mEdit: BEGIN                {call SystemEdit for DA editing & MultiFinder}
  1002.             IF (SystemEdit(menuItem-1)) THEN BEGIN
  1003.             END;
  1004.         END;
  1005.         
  1006.         mControl:
  1007.             CASE menuItem OF
  1008.                 iNoMPPClose: BEGIN            { toggle MPPClose selection flag to allow/prohibit .MPP closure }
  1009.                     IF (gATQEntry.globs^.allowClose) THEN BEGIN
  1010.                         gATQEntry.globs^.allowClose := FALSE;
  1011.                         SetItem(GetMHandle(mControl), iNoMPPClose, 'Allow .MPP Closure');
  1012.                     END
  1013.                     ELSE BEGIN
  1014.                         gATQEntry.globs^.allowClose := TRUE;
  1015.                         SetItem(GetMHandle(mControl), iNoMPPClose, 'Prohibit .MPP Closure');
  1016.                     END
  1017.                 END;
  1018.                 
  1019.                 iNoNameChange: BEGIN            { toggle Flagship Name change flag to allow/prohibit name change } 
  1020.                     IF (gATQEntry.globs^.allowChange) THEN BEGIN
  1021.                         gATQEntry.globs^.allowChange := FALSE;
  1022.                         SetItem(GetMHandle(mControl), iNoNameChange, 'Allow Name Change');
  1023.                     END
  1024.                     ELSE BEGIN
  1025.                         gATQEntry.globs^.allowChange := TRUE;
  1026.                         SetItem(GetMHandle(mControl), iNoNameChange, 'Prohibit Name Change');
  1027.                     END
  1028.                 END;
  1029.             END; { CASE menuItem of }
  1030.         
  1031.     END;    { CASE menuID OF }
  1032.     HiliteMenu(0);                    {unhighlight what MenuSelect (or MenuKey) hilited}
  1033. END; {DoMenuCommand}
  1034.  
  1035.  
  1036. {$S Main}
  1037. PROCEDURE DrawWindow(window: WindowPtr);
  1038.  
  1039. {Draw the contents of an application window.}
  1040.  
  1041. BEGIN
  1042.     SetPort(window);
  1043.     WITH window^ DO BEGIN
  1044.         EraseRect(portRect);        {as per TextEdit chapter of Inside Macintosh}
  1045.         DrawControls(window);        {this ordering makes for a better appearance}
  1046.         DrawGrowIcon(window);
  1047.         TEUpdate(portRect, DocumentPeek(window)^.docTE);
  1048.     END;
  1049. END; {DrawWindow}
  1050.  
  1051.  
  1052. {$S Main}
  1053. FUNCTION GetSleep: LONGINT;
  1054.  
  1055. {Calculate a sleep value for WaitNextEvent. This takes into account the things
  1056.  that DoIdle does with idle time.}
  1057.  
  1058. VAR
  1059.     sleep    : LONGINT;
  1060.     window    : WindowPtr;
  1061.  
  1062. BEGIN
  1063.     sleep := 60;                    {default value for sleep}
  1064.     IF NOT gInBackground THEN BEGIN            {if we are in front...}
  1065.         window := FrontWindow;            {and the front window is ours...}
  1066.         IF IsAppWindow(window) THEN BEGIN
  1067.             WITH DocumentPeek(window)^.docTE^^ DO
  1068.                 IF selStart = selEnd THEN    {and the selection is an insertion point...}
  1069.                     sleep := GetCaretTime;    {we need to blink the insertion point}
  1070.         END;
  1071.     END;
  1072.     GetSleep := sleep;
  1073. END; {GetSleep}
  1074.  
  1075.  
  1076. {$S Main}
  1077. PROCEDURE CommonAction(control: ControlHandle; VAR amount: INTEGER);
  1078.  
  1079. {Common algorithm for setting the new value of a control. It returns the actual amount
  1080. the value of the control changed. Note the pinning is done for the sake of returning
  1081. the amount the control value changed.}
  1082.  
  1083. VAR
  1084.     value, max    : INTEGER;
  1085.     window        : WindowPtr;
  1086. BEGIN
  1087.     value := GetCtlValue(control);    {get current value}
  1088.     max := GetCtlMax(control);        {and max value}
  1089.     amount := value - amount;
  1090.     IF amount < 0 THEN
  1091.         amount := 0
  1092.     ELSE IF amount > max THEN
  1093.         amount := max;
  1094.     SetCtlValue(control, amount);
  1095.     amount := value - amount;        {calculate true change}
  1096. END; {CommonAction}
  1097.  
  1098.  
  1099. {$S Main}
  1100. PROCEDURE VActionProc(control: ControlHandle; part: INTEGER);
  1101.  
  1102. {Determines how much to change the value of the vertical scrollbar by and how
  1103. much to scroll the TE record.}
  1104.  
  1105. VAR
  1106.     amount    : INTEGER;
  1107.     window    : WindowPtr;
  1108. BEGIN
  1109.     IF part <> 0 THEN BEGIN
  1110.         window := control^^.contrlOwner;
  1111.         WITH DocumentPeek(window)^, DocumentPeek(window)^.docTE^^ DO BEGIN
  1112.             CASE part OF
  1113.                 inUpButton, inDownButton:
  1114.                     amount := 1;                                                {one line}
  1115.                 inPageUp, inPageDown:
  1116.                     amount := (viewRect.bottom - viewRect.top) DIV lineHeight;    {one page}
  1117.             END;
  1118.             IF (part = inDownButton) | (part = inPageDown) THEN
  1119.                 amount := -amount;                                                {reverse direction}
  1120.             CommonAction(control, amount);
  1121.             IF amount <> 0 THEN
  1122.                 TEScroll(0, amount * docTE^^.lineHeight, docTE);
  1123.         END;
  1124.     END;
  1125. END; {VActionProc}
  1126.  
  1127.  
  1128. {$S Main}
  1129. PROCEDURE HActionProc(control: ControlHandle; part: INTEGER);
  1130.  
  1131. {Determines how much to change the value of the horizontal scrollbar by and how
  1132. much to scroll the TE record.}
  1133.  
  1134. VAR
  1135.     amount    : INTEGER;
  1136.     window    : WindowPtr;
  1137. BEGIN
  1138.     IF part <> 0 THEN BEGIN
  1139.         window := control^^.contrlOwner;
  1140.         WITH DocumentPeek(window)^, DocumentPeek(window)^.docTE^^ DO BEGIN
  1141.             CASE part OF
  1142.                 inUpButton, inDownButton:                                        {a few pixels}
  1143.                     amount := kButtonScroll;
  1144.                 inPageUp, inPageDown:
  1145.                     amount := viewRect.right - viewRect.left;                    {a page}
  1146.             END;
  1147.             IF (part = inDownButton) | (part = inPageDown) THEN
  1148.                 amount := -amount;                                                {reverse direction}
  1149.             CommonAction(control, amount);
  1150.             IF amount <> 0 THEN
  1151.                 TEScroll(amount, 0, docTE);
  1152.         END;
  1153.     END;
  1154. END; {HActionProc}
  1155.  
  1156.  
  1157. {$S Main}
  1158. PROCEDURE DoIdle;
  1159.  
  1160. {This is called whenever we get an null event or a mouse-moved event.
  1161.  It takes care of necessary periodic actions. For this program, it calls TEIdle.}
  1162.  
  1163. VAR
  1164.     window    : WindowPtr;
  1165.     myGRec: TransEventPtr;
  1166.     te            : TEHandle;
  1167.     strHdl1    : StringHandle;
  1168.     strHdl2    : StringHandle;
  1169.     strHdl3    : StringHandle;
  1170.     err        : OSErr;
  1171.     dtRec    : DateTimeRec;
  1172.     dtStr    : Str32;
  1173.     netStr    : Str255;
  1174.     i            : INTEGER;    { used to index into indEvtRec }
  1175.     
  1176.     FUNCTION GetMyStrings(evtNum : INTEGER; VAR s1, s2, s3 : StringHandle) : OSErr;
  1177.     
  1178.     BEGIN
  1179.         s1 := StringHandle(GetResource('STR ', evtNum));
  1180.         s2 := nil;
  1181.         s3 := nil;
  1182.         if (s1 <> NIL) THEN BEGIN
  1183.             CASE evtNum of
  1184.                 kATTransNameChangeTellTaskMsgNum,
  1185.                 kATTransNameChangeAskTaskMsgNum :
  1186.                     s2 := StringHandle(GetResource('STR ', kNewFlagshipNameMsg));
  1187.                 kATTransCancelNameChangeMsgNum :
  1188.                     s2 := StringHandle(GetResource('STR ', kCancelFlagshipNameMsg));
  1189.                 kATTransCableChangeMsgNum : BEGIN
  1190.                     s2 := StringHandle(GetResource('STR ', kLoCableRangeMsg));
  1191.                     s3 := StringHandle(GetResource('STR ', kHiCableRangeMsg));
  1192.                 END;
  1193.                 kATTransNetworkTransitionMsgNum : BEGIN
  1194.                     IF (gATQEntry.globs^.newConnFlag) THEN
  1195.                         s2 := StringHandle(GetResource('STR ', kNewConnection))
  1196.                     ELSE
  1197.                         s2 := StringHandle(GetResource('STR ', kCloseConnection));
  1198.                 END;
  1199.             END;
  1200.         END;
  1201.         GetMyStrings := ResError;
  1202.     END;
  1203.     
  1204.     FUNCTION DateRecToStr(dtRec : DateTimeRec) : Str255;
  1205.     
  1206.     VAR
  1207.         yearStr    : Str255;
  1208.         monthStr    : Str255;
  1209.         dayStr    : Str255;
  1210.         hourStr    : Str255;
  1211.         minStr    : Str255;
  1212.         secStr    : Str255;
  1213.         
  1214.     BEGIN
  1215.         NumToString(dtRec.year, yearStr);
  1216.         NumToString(dtRec.month, monthStr);
  1217.         NumToString(dtRec.day, dayStr);
  1218.         NumToString(dtRec.hour, hourStr);
  1219.         NumToString(dtRec.minute, minStr);
  1220.         NumToString(dtRec.second, secStr);
  1221.         dtStr := Concat(monthStr, '/', dayStr, '/', yearStr, '   ', hourStr, ':', minStr, ':', secStr);
  1222.         DateRecToStr := dtStr;
  1223.     END;
  1224.  
  1225.     PROCEDURE InsertStrOnNewLine(s : Str255; teHdl : TEHandle);
  1226.     
  1227.     VAR
  1228.         sLen    : LONGINT;
  1229.     
  1230.     BEGIN
  1231.         sLen := Length(s) + 1; { Get String length and add 1 for return char to be prepended to string }
  1232.         s[0] := Chr(kReturnChar);
  1233.         TEInsert(Ptr(@s), sLen, teHdl);     { Insert message into TE Rec }
  1234.     END;
  1235.     
  1236. BEGIN
  1237.     myGRec := gATQEntry.globs;
  1238.     i := myGRec^.nextEvt2Proc;
  1239.     IF (myGRec^.indEvtRec[i].evtMsgNum <> 0) THEN    { evtMsgNum is non-zero indicating that a Transition event }
  1240.                                                                                 { has occurred that we haven't processed this one yet }
  1241.     BEGIN
  1242.         WITH myGRec^ DO BEGIN
  1243.             Secs2Date(indEvtRec[i].evtTime, dtRec);
  1244.             dtStr := DateRecToStr(dtRec);
  1245.             if (docTE <> NIL) THEN BEGIN     { there is a window to output to }
  1246.                 err := GetMyStrings(indEvtRec[i].evtMsgNum, strHdl1, strHdl2, strHdl3);
  1247.                 if (err = noErr) THEN BEGIN
  1248.                     InsertStrOnNewLine(strHdl1^^, docTE);                        { Insert message into TE Rec }
  1249.                     TEInsert(Ptr(@dtStr[1]), LONGINT(dtStr[0]), docTE); { Insert dateTime stamp into TE Rec }
  1250.                     
  1251.                     CASE indEvtRec[i].evtMsgNum of
  1252.                         kATTransNameChangeTellTaskMsgNum,
  1253.                         kATTransNameChangeAskTaskMsgNum ,
  1254.                         kATTransCancelNameChangeMsgNum: 
  1255.                         BEGIN
  1256.                             InsertStrOnNewLine(strHdl2^^, docTE);                        { Insert message into TE Rec }
  1257.                             if (indEvtRec[i].evtMsgNum = kATTransCancelNameChangeMsgNum) THEN
  1258.                                 TEInsert(Ptr(@oldFlagName[1]), LONGINT(oldFlagName[0]), docTE)
  1259.                             ELSE
  1260.                                 TEInsert(Ptr(@newFlagName[1]), LONGINT(newFlagName[0]), docTE);
  1261.                         END;
  1262.  
  1263.                         kATTransCableChangeMsgNum :
  1264.                         BEGIN
  1265.                             InsertStrOnNewLine(strHdl2^^, docTE);                        { Insert message into TE Rec }
  1266.                             NumToString(newCableLo, netStr);                            { convert lo cable num to string }
  1267.                             TEInsert(Ptr(@netStr[1]), LONGINT(netStr[0]), docTE);
  1268.                             TEInsert(Ptr(@strHdl3^^[1]), LONGINT(strHdl3^^[0]), docTE);
  1269.                             NumToString(newCableHi, netStr);                            { convert lo cable num to string }
  1270.                             TEInsert(Ptr(@netStr[1]), LONGINT(netStr[0]), docTE);
  1271.                         END;
  1272.                         
  1273.                         kATTransNetworkTransitionMsgNum :
  1274.                         BEGIN
  1275.                             InsertStrOnNewLine(strHdl2^^, docTE);                        { Insert message into TE Rec }
  1276.                         END;
  1277.                     END;    { CASE }
  1278.                 END;        { err == noErr }
  1279.                 indEvtRec[i].evtMsgNum := 0;                            { indicate that we have processed this one }
  1280.                 IF (nextEvt2Proc >= kNumRecs) THEN
  1281.                     nextEvt2Proc := 1
  1282.                 ELSE
  1283.                     nextEvt2Proc := nextEvt2Proc + 1;
  1284.             END;    { docTE <> NIL }
  1285.         END;    { WITH }
  1286.     END; { myGRec^.indEvtRec[i].evtMsgNum <> 0 }
  1287. END; {DoIdle}
  1288.  
  1289.  
  1290. {$S Main}
  1291. PROCEDURE DoKeyDown(event: EventRecord);
  1292.  
  1293. {This is called for any keyDown or autoKey events, except when the
  1294.  Command key is held down. It looks at the frontmost window to decide what
  1295.  to do with the key typed.}
  1296.  
  1297. BEGIN
  1298. END; {DoKeyDown}
  1299.  
  1300.  
  1301. {$S Main}
  1302. PROCEDURE DoContentClick(window: WindowPtr; event: EventRecord);
  1303.  
  1304. {Called when a mouseDown occurs in the content of a window.}
  1305.  
  1306. VAR
  1307.     mouse        : Point;
  1308.     control        : ControlHandle;
  1309.     part, value    : INTEGER;
  1310.     shiftDown    : BOOLEAN;
  1311.     teRect        : Rect;
  1312.  
  1313. BEGIN
  1314.     IF IsAppWindow(window) THEN BEGIN
  1315.         SetPort(window);
  1316.         mouse := event.where;                                            {get the click position}
  1317.         GlobalToLocal(mouse);                                            {convert to local coordinates}
  1318.         {see if we are in the viewRect. if so, we won’t check the controls}
  1319.         GetTERect(window, teRect);
  1320.         part := FindControl(mouse, window, control);
  1321.         WITH DocumentPeek(window)^ DO
  1322.             CASE part OF
  1323.                 0:;                                            {do nothing for viewRect case}
  1324.                 inThumb: BEGIN
  1325.                     value := GetCtlValue(control);
  1326.                     part := TrackControl(control, mouse, NIL);
  1327.                     IF part <> 0 THEN BEGIN
  1328.                         value := value - GetCtlValue(control);
  1329.                         IF value <> 0 THEN
  1330.                             IF control = docVScroll THEN
  1331.                                 TEScroll(0, value * docTE^^.lineHeight, docTE)
  1332.                             ELSE
  1333.                                 TEScroll(value, 0, docTE);
  1334.                     END;
  1335.                 END;
  1336.                 OTHERWISE                                    {must be page or button}
  1337.                     IF control = docVScroll THEN
  1338.                         value := TrackControl(control, mouse, @VActionProc)
  1339.                     ELSE
  1340.                         value := TrackControl(control, mouse, @HActionProc);
  1341.             END;
  1342.     END;
  1343. END; {DoContentClick}
  1344.  
  1345.  
  1346. {$S Main}
  1347. PROCEDURE ResizeWindow(window: WindowPtr);
  1348.  
  1349. {Called when the window has been resized to fix up the controls and content}
  1350.  
  1351. BEGIN
  1352.     WITH window^ DO BEGIN
  1353.         AdjustScrollbars(window, TRUE);
  1354.         AdjustTE(window);
  1355.         InvalRect(portRect);
  1356.     END;
  1357. END; {ResizeWindow}
  1358.  
  1359.  
  1360. {$S Main}
  1361. PROCEDURE GetLocalUpdateRgn(window: WindowPtr; localRgn: RgnHandle);
  1362.  
  1363. {Returns the update region in local coordinates}
  1364.  
  1365. BEGIN
  1366.     CopyRgn(WindowPeek(window)^.updateRgn, localRgn);                        {save old update region}
  1367.     WITH window^.portBits.bounds DO
  1368.         OffsetRgn(localRgn, left, top);                                        {convert to local coords}
  1369. END; {GetLocalUpdateRgn}
  1370.  
  1371.  
  1372. {$S Main}
  1373. PROCEDURE DoGrowWindow(window: WindowPtr; event: EventRecord);
  1374.  
  1375. {Called when a mouseDown occurs in the grow box of an active window. In
  1376.  order to eliminate any 'flicker', we want to invalidate only what is
  1377.  necessary. Since ResizeWindow invalidates the whole portRect, we save
  1378.  the old TE viewRect, intersect it with the new TE viewRect, and
  1379.  remove the result from the update region. However, we must make sure
  1380.  that any old update region that might have been around gets put back.}
  1381.  
  1382. VAR
  1383.     growResult        : LONGINT;
  1384.     tempRect        : Rect;
  1385.     tempRgn            : RgnHandle;
  1386.     ignoreResult    : BOOLEAN;
  1387.  
  1388. BEGIN
  1389.     WITH screenBits.bounds DO
  1390.         SetRect(tempRect, kMinDocDim, kMinDocDim, right, bottom);            {set up limiting values}
  1391.     growResult := GrowWindow(window, event.where, tempRect);
  1392.     IF growResult <> 0 THEN                                                 {see if changed size}
  1393.         WITH DocumentPeek(window)^, window^ DO BEGIN
  1394.             tempRect := docTE^^.viewRect;                                    {save old text box}
  1395.             tempRgn := NewRgn;
  1396.             GetLocalUpdateRgn(window, tempRgn);                                {get localized update region}
  1397.             SizeWindow(window, LoWrd(growResult), HiWrd(growResult), TRUE);
  1398.             ResizeWindow(window);
  1399.             ignoreResult := SectRect(tempRect, docTE^^.viewRect, tempRect);    {find what stayed same}
  1400.             ValidRect(tempRect);                                            {take it out of update}
  1401.             InvalRgn(tempRgn);                                                {put back any prior update}
  1402.             DisposeRgn(tempRgn);
  1403.         END;
  1404. END; {DoGrowWindow}
  1405.  
  1406.  
  1407. {$S Main}
  1408. PROCEDURE DoZoomWindow(window: WindowPtr; part: INTEGER);
  1409.  
  1410. {Called when a mouseClick occurs in the zoom box of an active window.
  1411.  Everything has to get re-drawn here, so we don't mind that
  1412.  ResizeWindow invalidates the whole portRect.}
  1413.  
  1414. BEGIN
  1415.     WITH window^ DO BEGIN
  1416.         EraseRect(portRect);
  1417.         ZoomWindow(window, part, (window = FrontWindow));
  1418.         ResizeWindow(window);
  1419.     END;
  1420. END; {DoZoomWindow}
  1421.  
  1422.  
  1423. {$S Main}
  1424. PROCEDURE DoUpdate(window: WindowPtr);
  1425.  
  1426. {This is called when an update event is received for a window.
  1427.  It calls DrawWindow to draw the contents of an application window,
  1428.  but only if the visRgn is non-empty; for efficiency reasons,
  1429.  not because it is required.}
  1430.  
  1431. BEGIN
  1432.     IF IsAppWindow(window) THEN BEGIN
  1433.         BeginUpdate(window);                    {this sets up the visRgn}
  1434.         IF NOT EmptyRgn(window^.visRgn) THEN    {draw if updating needs to be done}
  1435.             DrawWindow(window);
  1436.         EndUpdate(window);
  1437.     END;
  1438. END; {DoUpdate}
  1439.  
  1440.  
  1441. {$S Main}
  1442. PROCEDURE DoActivate(window: WindowPtr; becomingActive: BOOLEAN);
  1443.  
  1444. {This is called when a window is activated or deactivated.}
  1445.  
  1446. VAR
  1447.     tempRgn, clipRgn    : RgnHandle;
  1448.     growRect            : Rect;
  1449.  
  1450. BEGIN
  1451.     IF IsAppWindow(window) THEN
  1452.         WITH DocumentPeek(window)^ DO
  1453.             IF becomingActive THEN BEGIN
  1454.                 {since we don’t want TEActivate to draw a selection in an area where
  1455.                  we’re going to erase and redraw, we’ll clip out the update region
  1456.                  before calling it.}
  1457.                 tempRgn := NewRgn;
  1458.                 clipRgn := NewRgn;
  1459.                 GetLocalUpdateRgn(window, tempRgn);            {get localized update region}
  1460.                 GetClip(clipRgn);
  1461.                 DiffRgn(clipRgn, tempRgn, tempRgn);            {subtract updateRgn from clipRgn}
  1462.                 SetClip(tempRgn);
  1463.                 TEActivate(docTE);                            {let TE do its thing}
  1464.                 SetClip(clipRgn);                            {restore the full-blown clipRgn}
  1465.                 DisposeRgn(tempRgn);
  1466.                 DisposeRgn(clipRgn);
  1467.  
  1468.                 {the controls need to be redrawn on activation:}
  1469.                 docVScroll^^.contrlVis := kControlVisible;
  1470.                 docHScroll^^.contrlVis := kControlVisible;
  1471.                 InvalRect(docVScroll^^.contrlRect);
  1472.                 InvalRect(docHScroll^^.contrlRect);
  1473.                 {the growbox needs to be redrawn on activation:}
  1474.                 growRect := window^.portRect;
  1475.                 WITH growRect DO BEGIN
  1476.                     top := bottom - kScrollbarAdjust;        {adjust for the scrollbars}
  1477.                     left := right - kScrollbarAdjust;
  1478.                     END;
  1479.                 InvalRect(growRect);
  1480.             END ELSE BEGIN
  1481.                 TEDeactivate(docTE);
  1482.                 {the controls should be hidden immediately on deactivation:}
  1483.                 HideControl(docVScroll);
  1484.                 HideControl(docHScroll);
  1485.                 {the growbox should be changed immediately on deactivation:}
  1486.                 DrawGrowIcon(window);
  1487.             END;
  1488. END; {DoActivate}
  1489.  
  1490.  
  1491. {$S Main}
  1492. PROCEDURE GetGlobalMouse(VAR mouse: Point);
  1493.  
  1494. {Get the global coordinates of the mouse. When you call OSEventAvail
  1495.  it will return either a pending event or a null event. In either case,
  1496.  the where field of the event record will contain the current position
  1497.  of the mouse in global coordinates and the modifiers field will reflect
  1498.  the current state of the modifiers. Another way to get the global
  1499.  coordinates is to call GetMouse and LocalToGlobal, but that requires
  1500.  being sure that thePort is set to a valid port.}
  1501.  
  1502. VAR
  1503.     event    : EventRecord;
  1504.     
  1505. BEGIN
  1506.     IF OSEventAvail(kNoEvents, event) THEN;    {we aren't interested in any events}
  1507.     mouse := event.where;                    {just the mouse position}
  1508. END;
  1509.  
  1510.  
  1511. {$S Main}
  1512. PROCEDURE AdjustCursor(mouse: Point; region: RgnHandle);
  1513.  
  1514. {Change the cursor's shape, depending on its position. This also calculates the region
  1515.  where the current cursor resides (for WaitNextEvent). If the mouse is ever outside of
  1516.  that region, an event is generated, causing this routine to be called. This
  1517.  allows us to change the region to the region the mouse is currently in. If
  1518.  there is more to the event than just “the mouse moved”, we get called before the
  1519.  event is processed to make sure the cursor is the right one. In any (ahem) event,
  1520.  this is called again before we fall back into WNE.}
  1521.  
  1522.  
  1523. VAR
  1524.     window        : WindowPtr;
  1525.     arrowRgn    : RgnHandle;
  1526.     iBeamRgn    : RgnHandle;
  1527.     iBeamRect    : Rect;
  1528.  
  1529. BEGIN
  1530.     window := FrontWindow;    {we only adjust the cursor when we are in front}
  1531.     IF (NOT gInBackground) AND (NOT IsDAWindow(window)) THEN BEGIN
  1532.         {calculate regions for different cursor shapes}
  1533.         arrowRgn := NewRgn;
  1534.         iBeamRgn := NewRgn;
  1535.  
  1536.         {start with a big, big rectangular region}
  1537.         SetRectRgn(arrowRgn, kExtremeNeg, kExtremeNeg, kExtremePos, kExtremePos);
  1538.  
  1539.         {calculate iBeamRgn}
  1540.         IF IsAppWindow(window) THEN BEGIN
  1541.             iBeamRect := DocumentPeek(window)^.docTE^^.viewRect;
  1542.             SetPort(window);                    {make a global version of the viewRect}
  1543.             WITH iBeamRect DO BEGIN
  1544.                 LocalToGlobal(topLeft);
  1545.                 LocalToGlobal(botRight);
  1546.             END;
  1547.             RectRgn(iBeamRgn, iBeamRect);
  1548.             WITH window^.portBits.bounds DO
  1549.                 SetOrigin(-left, -top);
  1550.             SectRgn(iBeamRgn, window^.visRgn, iBeamRgn);
  1551.             SetOrigin(0, 0);
  1552.         END;
  1553.  
  1554.         {subtract other regions from arrowRgn}
  1555.         DiffRgn(arrowRgn, iBeamRgn, arrowRgn);
  1556.         
  1557.         {change the cursor and the region parameter}
  1558.         IF PtInRgn(mouse, iBeamRgn) THEN BEGIN
  1559.             SetCursor(GetCursor(iBeamCursor)^^);
  1560.             CopyRgn(iBeamRgn, region);
  1561.         END ELSE BEGIN
  1562.             SetCursor(arrow);
  1563.             CopyRgn(arrowRgn, region);
  1564.         END;
  1565.  
  1566.         {get rid of our local regions}
  1567.         DisposeRgn(arrowRgn);
  1568.         DisposeRgn(iBeamRgn);
  1569.     END;
  1570. END; {AdjustCursor}
  1571.  
  1572.  
  1573. {$S Main}
  1574. PROCEDURE DoEvent(event: EventRecord);
  1575.  
  1576. {Do the right thing for an event. Determine what kind of event it is, and call
  1577.  the appropriate routines.}
  1578.  
  1579. VAR
  1580.     part, err    : INTEGER;
  1581.     window        : WindowPtr;
  1582.     key            : CHAR;
  1583.     ignore        : BOOLEAN;
  1584.     aPoint        : Point;
  1585.  
  1586. BEGIN
  1587.     CASE event.what OF
  1588.         nullEvent:
  1589.             DoIdle;
  1590.         mouseDown: BEGIN
  1591.             part := FindWindow(event.where, window);
  1592.             CASE part OF
  1593.                 inMenuBar: BEGIN
  1594.                     AdjustMenus;
  1595.                     DoMenuCommand(MenuSelect(event.where));
  1596.                 END;
  1597.                 inSysWindow:
  1598.                     SystemClick(event, window);
  1599.                 inContent:
  1600.                     IF window <> FrontWindow THEN BEGIN
  1601.                         SelectWindow(window);
  1602.                         {DoEvent(event);}    {use this line for "do first click"}
  1603.                     END ELSE
  1604.                         DoContentClick(window, event);
  1605.                 inDrag:
  1606.                     DragWindow(window, event.where, screenBits.bounds);
  1607.                 inGrow:
  1608.                     DoGrowWindow(window, event);
  1609.                 inGoAway:
  1610.                     IF TrackGoAway(window, event.where) THEN
  1611.                         ignore := DoCloseWindow(window);        {we don't care if cancelled}
  1612.                 inZoomIn, inZoomOut:
  1613.                     IF TrackBox(window, event.where, part) THEN
  1614.                         DoZoomWindow(window, part);
  1615.             END;
  1616.         END;
  1617.         keyDown, autoKey: BEGIN
  1618.             key := CHR(BAnd(event.message, charCodeMask));
  1619.             IF BAnd(event.modifiers, cmdKey) <> 0 THEN BEGIN    {Command key down}
  1620.                 IF event.what = keyDown THEN BEGIN
  1621.                     AdjustMenus;            {enable/disable/check menu items properly}
  1622.                     DoMenuCommand(MenuKey(key));
  1623.                 END;
  1624.             END;
  1625.         END;                                {call DoActivate with the window and...}
  1626.         activateEvt:                        {TRUE for activate, FALSE for deactivate}
  1627.             DoActivate(WindowPtr(event.message), BAnd(event.modifiers, activeFlag) <> 0);
  1628.         updateEvt:                            {call DoUpdate with the window to update}
  1629.             DoUpdate(WindowPtr(event.message));
  1630.         {1.01 - It is not a bad idea to at least call DIBadMount in response
  1631.          to a diskEvt, so that the user can format a floppy.}
  1632.         diskEvt:
  1633.             IF HiWrd(event.message) <> noErr THEN BEGIN
  1634.                 SetPt(aPoint, kDILeft, kDITop);
  1635.                 err := DIBadMount(aPoint, event.message);
  1636.             END;
  1637.         kOSEvent:
  1638.             CASE BAnd(BRotL(event.message, 8), $FF) OF    {high byte of message}
  1639.                 kMouseMovedMessage:
  1640.                     DoIdle;                    {mouse moved is also an idle event}
  1641.                 kSuspendResumeMessage: BEGIN
  1642.                     gInBackground := BAnd(event.message, kResumeMask) = 0;
  1643.                     DoActivate(FrontWindow, NOT gInBackground);
  1644.                 END;
  1645.             END;
  1646.     END;
  1647. END; {DoEvent}
  1648.  
  1649.  
  1650. {$S Main}
  1651. PROCEDURE EventLoop;
  1652.  
  1653. {Get events forever, and handle them by calling DoEvent.
  1654.  Also call AdjustCursor each time through the loop.}
  1655.  
  1656. VAR
  1657.     cursorRgn    : RgnHandle;
  1658.     gotEvent    : BOOLEAN;
  1659.     event        : EventRecord;
  1660.     mouse        : Point;
  1661.  
  1662. BEGIN
  1663.     cursorRgn := NewRgn;        {we'll pass an empty region to WNE the first time thru}
  1664.     REPEAT
  1665.         IF gHasWaitNextEvent THEN BEGIN    {put us 'asleep' forever under MultiFinder}
  1666.             GetGlobalMouse(mouse);        {since we might go to sleep}
  1667.             AdjustCursor(mouse, cursorRgn);
  1668.             gotEvent := WaitNextEvent(everyEvent, event, GetSleep, cursorRgn);
  1669.         END ELSE BEGIN
  1670.             SystemTask;                    {must be called if using GetNextEvent}
  1671.             gotEvent := GetNextEvent(everyEvent, event);
  1672.         END;
  1673.         IF gotEvent THEN BEGIN
  1674.             AdjustCursor(event.where, cursorRgn);    {make sure we have the right cursor}
  1675.             DoEvent(event);                {Handle the event only if for us.}
  1676.             END
  1677.         ELSE
  1678.             DoIdle;
  1679.         {If you are using modeless dialogs that have editText items,
  1680.          you will want to call IsDialogEvent to give the caret a chance
  1681.          to blink, even if WNE/GNE returned FALSE. However, check FrontWindow
  1682.          for a non-NIL value before calling IsDialogEvent.}
  1683.     UNTIL FALSE;    {loop forever}
  1684. END; {EventLoop}
  1685.  
  1686.  
  1687. PROCEDURE _DataInit; EXTERNAL;
  1688.  
  1689. {This routine is automatically linked in by the MPW Linker. This external
  1690.  reference to it is done so that we can unload its segment, %A5Init.}
  1691.  
  1692.  
  1693. {$S Main}
  1694. BEGIN
  1695.     UnloadSeg(@_DataInit);    {note that _DataInit must not be in Main!}
  1696.     
  1697.     {1.01 - call to ForceEnvirons removed}
  1698.     {If you have stack requirements that differ from the default,
  1699.      then you could use SetApplLimit to increase StackSpace at 
  1700.      this point, before calling MaxApplZone.}
  1701.      
  1702.     MaxApplZone;            {expand the heap so code segments load at the top}
  1703.  
  1704.     Initialize;                {initialize the program}
  1705.     UnloadSeg(@Initialize);    {note that Initialize must not be in Main!}
  1706.  
  1707.     EventLoop;                {call the main event loop}
  1708. END.
  1709.